// 示例方法，没有实际意义
export function trim(str: string) {
  return str.trim();
}

// 过滤看不到的字符串
const filterUnSeenUnicode = (str: string) => {
  let str1 = zhToUnicode(str);
  let str2 = trimNoPrintable(str1);
  let finalStr = unicodeToZh(str2);
  return finalStr;
};

// 1、中文转unicode
const zhToUnicode = (oStr: string) => {
  let str = '';
  for (let i = 0; i < oStr.length; i++) {
    let temp = oStr.charCodeAt(i).toString(16);
    while (temp.length < 4) {
      temp = '0' + temp;
    }
    str += '\\u' + temp;
  }
  return str;
};

// 2、去除unicode不可见字符
const trimNoPrintable = (oStr: string) => {
  let result = oStr.replace(/\\u2006|\\u202a|\\u0008|\\u0009|\\u000a|\\u000b|\\u000c|\\u000d|\\u2028|\\u2029|\\ufeff|\\u200e|\\u200d/g, '');
  return result;
};

// 3、unicode转中文
const unicodeToZh = (oStr: string) => {
  let str = '';
  let s = oStr.split('\\u');
  for (let index = 0; index < s.length; index++) {
    if (s[index] === '') {
      s.splice(index, 1);
    }
  }
  for (let i = 0; i < s.length; i++) {
    str += String.fromCharCode(parseInt(s[i], 16));
  }
  return str;
};

/**
 * 将请求格式化成 pro-table 使用的 request 格式
 * @param {Function} method 调用请求
 * @param {Object} params 调用请求
 * @return {Promise}
 */
export function formatTableRequest(method: Function, params: object, query?: object | undefined, options?: IFormatOptions): Promise<any> {
  if (!query) query = params;
  return new Promise((resolve, reject) => {
    method(query, params)
      .then((res: any) => {
        const resList = res.data?.records || res.data || [];
        const newList = options?.formatList ? options.formatList(resList) : resList;
        resolve({
          success: res.success,
          data: newList,
          total: res.data?.total || 0,
        });
      })
      .catch((err: Error) => {
        reject(err);
      });
  });
}

// 格式化请求分数数据
export function formatPaginationParams(
  params: {
    current?: number;
    pageSize?: number;
    [key: string]: any;
  } = { current: 1, pageSize: 10 },
) {
  return {
    ...params,
  };
}